<template>
  <div>
    <el-table
      :data="displayData"
      style="width: 100%">
      <el-table-column
        prop="Id"
        label="Id">
      </el-table-column>

      <el-table-column
        prop="Name"
        label="Name">
      </el-table-column>

       <el-table-column
        prop="Gold"
        label="Gold">
      </el-table-column>

       <el-table-column
        prop="Gem"
        label="Gem">
      </el-table-column>

       <el-table-column
        prop="Level"
        label="Level">
      </el-table-column>

       <el-table-column
        prop="Exp"
        label="Exp">
      </el-table-column>

       <el-table-column
        prop="Server"
        label="Server">
      </el-table-column>

       <el-table-column
        prop="Created"
        label="Created">
      </el-table-column>

       <el-table-column
        prop="Updated"
        label="Updated">
      </el-table-column>

      <el-table-column label="Actions" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleEdit(row)">
            Edit
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">
            Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      v-model="editDialogVisible"
      title="编辑数据"
      width="30%">
      <el-form :model="form" label-width="80px">
        <el-form-item label="Id">
          <el-input v-model="form.Id" disabled></el-input>
        </el-form-item>
        <el-form-item label="Name">
          <el-input v-model="form.Name"></el-input>
        </el-form-item>
        <el-form-item label="Gold">
          <el-input v-model.number="form.Gold"></el-input>
        </el-form-item>
        <el-form-item label="Gem">
          <el-input v-model.number="form.Gem"></el-input>
        </el-form-item>
        <el-form-item label="Level">
          <el-input v-model.number="form.Level"></el-input>
        </el-form-item>
        <el-form-item label="Exp">
          <el-input v-model.number="form.Exp"></el-input>
        </el-form-item>
        <el-form-item label="Server">
          <el-input v-model.number="form.Server"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="confirmEdit">确认</el-button>
        </span>
      </template>
    </el-dialog>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { userinfo } from '@/api/user'
export default {
  name: 'Userinfo',
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      total: 0,
      displayData: [],
      editDialogVisible: false,
      deleteDialogVisible: false,
      form: {},
      currentRow: null
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      userinfo().then((res) => {
        console.log(res)
        this.tableData = res.data
        this.total = this.tableData.length
        this.handlePageChange()
      }).catch((err) => {
        console.log(err)
      })
    },
    handlePageChange() {
      const startIndex = (this.currentPage - 1) * this.pageSize
      const endIndex = startIndex + this.pageSize
      this.displayData = this.tableData.slice(startIndex, endIndex)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.handlePageChange()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.handlePageChange()
    },
    handleEdit(row) {
      this.currentRow = row
      this.form = { ...row }
      this.editDialogVisible = true
    }
//     confirmEdit() {
//   // 160 行：发送请求到后端更新数据
//     axios.put('/api/your-update-url', this.form) // 假设是 PUT 请求，/api/your-update-url 替换为实际后端接口地址
//     .then(response => {
//       // 161 行：假设更新成功，更新表格中的数据
//       const index = this.displayData.findIndex(item => item.Id === this.currentRow.Id)
//       if (index !== -1) {
//         this.displayData.splice(index, 1, this.form)
//       }
//       this.editDialogVisible = false
//     })
//     .catch(error => {
//       console.error('更新数据失败：', error)
//       // 可添加失败提示，比如 this.$message.error('更新失败，请重试')
//     })
// }
//       // 这里可以发送请求到后端更新数据
//       // 假设更新成功，更新表格中的数据
//       const index = this.displayData.findIndex(item => item.Id === this.currentRow.Id)
//       if (index !== -1) {
//         this.displayData.splice(index, 1, this.form)
//       }
//       this.editDialogVisible = false
//     }

  }
}
</script>

<style scoped>
</style>
